package com.rjkj.medicalwaste.ui.xlb

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.base.BaseFragment
import com.rjkj.medicalwaste.databinding.FragmentXlbTestBinding
import com.rjkj.medicalwaste.utils.BlueDeviceHelper
import com.rjkj.medicalwaste.utils.Encoder03Small
import com.rjkj.medicalwaste.viewmodels.BluetoothControlViewModel
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class XlbTestFragment : BaseFragment<FragmentXlbTestBinding>() {
    private val xlbTestViewModel: XlbTestViewModel by viewModels{
        MainViewModelProvider.providerXlbTestModel(requireContext())
    }
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }
    private val bluetoothControlViewModel: BluetoothControlViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentXlbTestBinding.inflate(inflater, container, false)
        onSubscribeUi(binding)
        binding.lifecycleOwner = viewLifecycleOwner
        binding.data = xlbTestViewModel
        return binding.root
    }
    fun printTestData(){

        val arrayOf = arrayOf(
                "2ABCDEF000003A17",
        )
        lifecycleScope.launch(Dispatchers.IO){
            for (s in arrayOf.indices) {
                val encoder03Small = Encoder03Small()
                //袋模式1，箱模式：2
                encoder03Small.setQRcodeTest("1", arrayOf[s], "00000000")
                encoder03Small.setTestGarbage("",
                        arrayOf[s],
                        (s+1).toString(),
                        "",
                        "",
                        "",
                        "")
                bluetoothControlViewModel.printRecTest(encoder03Small)
                delay(1000L)
                bluetoothControlViewModel.printOnce()
                delay(1000L)
            }

        }



    }
    override fun onSubscribeUi(binding: FragmentXlbTestBinding) {
        binding.buttonGetAllData.setOnClickListener {
            xlbTestViewModel.getAllUserData(userViewModel.getSerial())
        }
        binding.btDelete.setOnClickListener{
            xlbTestViewModel.deleteAllGarbageData()
        }
        binding.buttonShowAllData.setOnClickListener {
            findNavController().navigate(R.id.action_xlbTestFragment_to_allUserDataFragment)
        }
        binding.button3.setOnClickListener{
            printTestData()
        }
        binding.button5.setOnClickListener {
//            xlbTestViewModel.testAPI()
            throw IllegalAccessException("xlb测试")
        }
        initTip(xlbTestViewModel)
//        binding.button.setOnClickListener {
////            Log.e(TAG, "onSubscribeUi: ${binding.v2.width}", )
////            val flingAnimation= FlingAnimation(it, DynamicAnimation.X)
////            val pixelPerSecond = TypedValue.applyDimension(
////                TypedValue.COMPLEX_UNIT_DIP, -100f,
////                resources.displayMetrics
////            )
////            flingAnimation.friction = 0f
////            Log.e(TAG, "onSubscribeUi:$pixelPerSecond ", )
////            flingAnimation.setStartVelocity(pixelPerSecond)
////            flingAnimation.start()
//        }
    }


}